/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.dbg.view.content.ease {
	import com.googlecode.milib.animation.static_.MLEase;
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.dbg.view.content.ease.EaseTypeInfo;
	import com.googlecode.milib.display.DuplicatedMCSInClasses;
	import com.googlecode.milib.display.IClassForMCFactory;
	import com.googlecode.milib.display.IMCSCollectionController;
	import com.googlecode.milib.display.MCSPositioning;
	import com.googlecode.milib.display.service.MLMC;
	import com.googlecode.milib.tools.MLEventsReflector;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.display.MovieClip;

	/**
	 * @author Marek Brun
	 */
	internal class EasePropsSliders extends MLBroadcastClass implements IMCSCollectionController, IClassForMCFactory, IMLBroadcasterListener {
		
		public const event_RequestChange : String = 'RequestChange';		public const event_Changed : String = 'Changed';
		
		public function get event_MCSSumChanged():String { return 'MCSSumChanged'; }
		
		private var mlmc:MLMC;
		private var distance:Number;
		private var positioner:MCSPositioning;
		private var easeInfo:EaseTypeInfo;
		private var propSlidersEvents:MLEventsReflector;
		private var model:MLEase;
		private var classes:DuplicatedMCSInClasses;
		
		public function EasePropsSliders(mc:MovieClip) {
			mlmc=MLMC.forInstance(mc);
			
			distance=mlmc.getMC('ele1').y-mlmc.getMC('ele0').y;
			mlmc.mc.removeChild(mlmc.getMC('ele1'));
			
			positioner=new MCSPositioning(this, true);
			classes=new DuplicatedMCSInClasses(this, mlmc.getMC('ele0'));
			
			propSlidersEvents=new MLEventsReflector(true);
			propSlidersEvents.addListener(this);
			
		}
		
		public function setEaseInfo(easeInfo:EaseTypeInfo):void {
			this.easeInfo=easeInfo;
			classes.getDuplicator().setMCSNumber(easeInfo.params.length);
			var propSliders:Array=classes.getCurrentMCSInClasses();
			var i:uint;
			var loop:EasePropSlider;
			for(i=0;i<propSliders.length;i++){
				loop=propSliders[i];
				loop.setParams(easeInfo.params[i]);
			}
			positioner.draw();
		}
		
		public function setEase(model:MLEase):void {
			this.model=model;
			var easeProps:Array=model.getEaseProps();
			var i:uint;
			var loop:Number;
			propSlidersEvents.removeListener(this);
			for(i=0;i<easeProps.length;i++){
				loop=easeProps[i];
				EasePropSlider(classes.getClassWithMCByIndex(i)).getSlider().getModel().v=loop;
			}
			propSlidersEvents.addListener(this);
		}
		
		public function get mc():MovieClip { return mlmc.mc; };
		
		public function getMCS():Array {
			return classes.getDuplicator().getCurrentVisibleMCS();
		}
		
		public function getMCWidth(mc:MovieClip):Number {
			return NaN;
		}
		
		public function getMCHeight(mc:MovieClip):Number {
			return distance;
		}
		
		public function createClassForMC(requestObj:Object, mc:MovieClip, index:uint):Object {
			var easeProp:EasePropSlider=new EasePropSlider(mc);
			propSlidersEvents.addObject(easeProp, index);
			return easeProp;
		}
		
//********************************************************************************************
//		events for EasePropsSliders
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case propSlidersEvents:
					if(ev.event!=propSlidersEvents.event_EventReflection){ break; }
					ev=ev.data;
					var hero:EasePropSlider=EasePropSlider(ev.hero);
					switch(ev.event) {
						case hero.event_Changed:
							if(model){
								propSlidersEvents.getDataByObject(hero);
								var easeProps:Array=model.getEaseProps();
								easeProps[propSlidersEvents.getDataByObject(hero)]=hero.getSlider().getModel().v;
								model.setEaseProps(easeProps);
								broadcastEvent(event_RequestChange);
							}
						break;
					}
				break;
			}
		}
	}
}
